using System;
using System.Collections.Generic;
using UnityEngine;

public class TableTennisPlayerBase : MonoBehaviour
{
	public struct MoveToPositionPlan
	{
		public delegate void OnMoveDone();

		public bool isActive;

		public Vector3 desiredPosition;

		public float timeToGetToPosition;

		public HitAnimationInfo hitAnimation;

		public Vector3 positionWhenStartMove;

		public float ellapsedTime;

		public float hitFlightTime;

		public OnMoveDone onMoveDone;

		public void MoveToPosition(Vector3 startPosition, Vector3 desiredPosition, HitAnimationInfo hitAnimation, float timeToGetToPosition, float hitFlightTime, OnMoveDone onMoveDone = null)
		{
			this.desiredPosition = desiredPosition;
			this.hitAnimation = hitAnimation;
			this.timeToGetToPosition = timeToGetToPosition;
			positionWhenStartMove = startPosition;
			this.hitFlightTime = hitFlightTime;
			this.onMoveDone = onMoveDone;
			ellapsedTime = 0f;
			isActive = true;
		}
	}

	public PlayerAnimationsController animController;

	public float symDeltaTime = 0.1f;

	public bool canAlwaysHit;

	public bool canHitNow;

	[NonSerialized]
	public Transform serveHand;

	[NonSerialized]
	public LoopParams loopParams = new LoopParams();

	private KineticPlan _kineticPlan;

	public MoveToPositionPlan movePlan;

	public float skillPoints = 2f;

	public List<AudioSource> audioSources = new List<AudioSource>();

	public int teamTag { get; protected set; }

	public int tableSide { get; protected set; }

	public TableTennisMatchController match { get; protected set; }

	public KineticPlan kineticPlan
	{
		get
		{
			if (_kineticPlan == null)
			{
				for (int i = 0; i < base.transform.childCount; i++)
				{
					Transform child = base.transform.GetChild(i);
					_kineticPlan = child.GetComponent<KineticPlan>();
					if (_kineticPlan != null)
					{
						break;
					}
				}
			}
			if (_kineticPlan == null && base.transform.childCount == 1)
			{
				Transform child2 = base.transform.GetChild(0);
				_kineticPlan = child2.gameObject.AddComponent<KineticPlan>();
			}
			return _kineticPlan;
		}
	}

	public Vector3 playerPosition
	{
		get
		{
			if (kineticPlan == null)
			{
				return Vector3.zero;
			}
			return kineticPlan.cachedTransform.position;
		}
		set
		{
			if (!(kineticPlan == null))
			{
				kineticPlan.cachedTransform.position = value;
			}
		}
	}

	public virtual Vector3 playerPos()
	{
		return playerPosition;
	}

	public virtual bool IsInServe()
	{
		return false;
	}

	public virtual float GetSkillPoints(PlayerStatsManager.SkillType skillType)
	{
		return skillPoints;
	}

	public void PlaySound(AudioClip clip, float volume = 1f)
	{
		AudioSource audioSource = audioSources[0];
		foreach (AudioSource audioSource2 in audioSources)
		{
			if (!audioSource2.isPlaying)
			{
				audioSource = audioSource2;
				break;
			}
		}
		audioSource.clip = clip;
		audioSource.volume = volume;
		audioSource.loop = false;
		audioSource.Play();
	}

	public void SetMatch(TableTennisMatchController match)
	{
		this.match = match;
		if (!(kineticPlan != null))
		{
		}
	}

	public float GetSkillDifference(PhysicsBall ball, float power, HitParamsDB.HitType hitType, PlayerStatsManager.SkillType skillType)
	{
		HitParamsDB.Hit hit = ScriptableObjectSingleton<HitParamsDB>.instance.GetHit(hitType);
		HitParamsDB.HitRange range = hit.GetRange(power);
		HitParameters hitParams = ball.hitParams;
		float num = GetSkillPoints(skillType);
		float attackPoints = hitParams.attackPoints;
		float num2 = num - attackPoints;
		if (num2 < 0f)
		{
			num2 *= hitParams.reductionFactor;
		}
		num2 += range.addDefenseSkillPoints + hitParams.opponentDefensePoints;
		num2 = num2 * MatchParameters.InitParameters.skillPointsScale + MatchParameters.InitParameters.skillPointsOffset;
		return MatchParameters.InitParameters.maxSkillPointsDifference.Clamp(num2);
	}

	public virtual void Init(int teamTag, int tableSide, TableTennisMatchController match, bool canAlwaysHit)
	{
		if (kineticPlan != null)
		{
			kineticPlan.match = match;
		}
		this.teamTag = teamTag;
		this.tableSide = tableSide;
		this.match = match;
		this.canAlwaysHit = canAlwaysHit;
		canHitNow = false;
		if (animController != null)
		{
			animController.match = match;
		}
		int num = 3;
		Transform transform = base.transform.Search("RUBBER001");
		if (transform == null)
		{
			transform = base.transform;
		}
		while (audioSources.Count < num)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(match.starter.playerAudioPrefab);
			gameObject.transform.parent = transform;
			gameObject.transform.localPosition = Vector3.zero;
			gameObject.SetActive(true);
			audioSources.Add(gameObject.GetComponent<AudioSource>());
		}
	}

	public virtual void Reset()
	{
		if (kineticPlan != null)
		{
			kineticPlan.CancelAll();
		}
	}

	public virtual void StartServe(PhysicsBall ball)
	{
	}

	public virtual void OpponentWillMissShot(TableTennisPlayerBase opponent)
	{
	}

	public virtual void OpponentChosenHitPlan(TableTennisPlayerBase opponent, PlayerAnimationsController.HitPlan hitPlan)
	{
	}

	public virtual void AwaitServe()
	{
	}

	public virtual void ReceiveBall(PhysicsBall ball)
	{
	}

	public virtual void BallRemovedFromGame()
	{
	}
}
